import * as THREE from 'three';

// 创建高亮材质
export const createHighlightMaterial = (): THREE.MeshBasicMaterial => {
  return new THREE.MeshBasicMaterial({
    color: 0x00ff00,
    transparent: true,
    opacity: 0.8,
    wireframe: false
  });
};

// 检查对象是否可选择
export const isObjectSelectable = (object: THREE.Object3D): boolean => {
  if (!object.name) return false;
  
  // 监测点
  if (object.name.startsWith('point_')) return true;
  // 屏蔽区域
  if (object.name.startsWith('shield_area_')) return true;
  // 模型
  if (object.name === 'output0') return true;
  
  return false;
};

// 获取对象的类型
export const getObjectType = (object: THREE.Object3D): 'point' | 'shield' | 'model' | 'unknown' => {
  if (object.name.startsWith('point_')) return 'point';
  if (object.name.startsWith('shield_area_')) return 'shield';
  if (object.name === 'output0') return 'model';
  return 'unknown';
};

// 计算对象边界框
export const calculateObjectBoundingBox = (object: THREE.Object3D): THREE.Box3 => {
  const box = new THREE.Box3().setFromObject(object);
  return box;
};

// 计算聚焦位置和距离
export const calculateFocusPosition = (
  object: THREE.Object3D, 
  camera: THREE.PerspectiveCamera
): { position: THREE.Vector3; target: THREE.Vector3 } => {
  const box = calculateObjectBoundingBox(object);
  const center = box.getCenter(new THREE.Vector3());
  const size = box.getSize(new THREE.Vector3());
  const maxDim = Math.max(size.x, size.y, size.z);
  
  const fov = camera.fov * (Math.PI / 180);
  const distance = Math.max(maxDim * 1.5, 100);
  
  const position = center.clone();
  position.z += distance;
  
  return { position, target: center };
};